package cn.kgc.maoqichao.microservicecloudproviderdept8001.controller;

import cn.kgc.maoqichao.microservicecloudproviderdept8001.pojo.Project;
import cn.kgc.maoqichao.microservicecloudproviderdept8001.pojo.ResultEntity;
import cn.kgc.maoqichao.microservicecloudproviderdept8001.service.ProjectService;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/provider/project")
public class ProjectController {
    @Resource
    ProjectService projectService;

    @Resource
    DiscoveryClient client;

    @RequestMapping("/list")
    public ResultEntity<List<Project>> list(){
        List<Project> all = projectService.getAll();
        return ResultEntity.successWithoutData(all);
    }

    @RequestMapping("/get/{id}")
    public ResultEntity<Project> get(@PathVariable Integer id){
        Project project = projectService.getById(id);
        return ResultEntity.successWithoutData(project);
    }

    @RequestMapping("/update")
    public ResultEntity<String> update(@RequestBody Project project){
        int i = projectService.updateByPrimaryKey(project);
        if (i>0){
            return ResultEntity.successWithoutData();
        }else{
            return ResultEntity.failed("修改失败");
        }
    }

    @RequestMapping("/insert")
    public ResultEntity<String> insert(@RequestBody Project project){
        int i = projectService.insert(project);
        if (i>0){
            return ResultEntity.successWithoutData();
        }else{
            return ResultEntity.failed("添加失败");
        }
    }

    @RequestMapping("/delete/{id}")
    public ResultEntity<String> delete(@PathVariable Integer id){
        int i=projectService.deleteByPrimaryKey(id);
        if (i>0){
            return ResultEntity.successWithoutData();
        }else{
            return ResultEntity.failed("删除失败");
        }
    }
}
